package com.example.studentmanagement;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.Window;

import com.example.studentmanagement.fragment.HomeFragment;

public class MainActivity extends FragmentActivity {
	
	private Fragment mContent;
	private FragmentManager mFragmentManager;
	private FragmentTransaction mFragmentTrans;
	private static final String FRAGMENT_TAG = "fragment";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE); 
		setContentView(R.layout.activity_main);
		if (savedInstanceState != null)
			mContent = getSupportFragmentManager().getFragment(savedInstanceState, "mContent");
		if (mContent == null) {
			mContent = HomeFragment.makeInstance();
		}
		
		setContentView(R.layout.activity_main);
		//for the first time
		mFragmentManager = getSupportFragmentManager();
		mFragmentTrans = mFragmentManager.beginTransaction();
		mFragmentTrans.replace(R.id.fragment_content, mContent, FRAGMENT_TAG);
		mFragmentTrans.addToBackStack(null);
		mFragmentTrans.commit();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	public void switchContent(Fragment fragment) {
		mContent = fragment;
		mFragmentTrans = mFragmentManager.beginTransaction();
		mFragmentTrans.setCustomAnimations(R.anim.slide_left_in, 
				R.anim.slide_left_out, 
				R.anim.slide_right_in,
	            R.anim.slide_right_out);
		mFragmentTrans.replace(R.id.fragment_content, fragment, FRAGMENT_TAG);   
		mFragmentTrans.addToBackStack(null);
		mFragmentTrans.commit();
	}

}
